#+TITLE:     Configuración de Emacs usando Org-Babel
#+AUTHOR:    arkhan
#+EMAIL:     e@ibañez.org
#+DATE:      2016-12-11
#+LANGUAGE:  es
#+OPTIONS: author:nil date:nil toc:nil title:nil e:nil
#+PROPERTY: header-args :tangle init.el :comments org
#+LaTeX_HEADER: \pagenumbering{gobble}
#+LaTeX_HEADER: \usepackage[T1]{fontenc}
#+LaTeX_HEADER: \usepackage{fontspec}
#+LaTeX_HEADER: \setmonofont[Scale=0.7]{DejaVu Sans Mono}
#+LaTeX_HEADER: \usepackage{mathpazo}
#+LaTeX_HEADER: \usepackage{geometry}
#+LaTeX_HEADER: \geometry{a4paper, margin=20mm}
#+LaTeX_HEADER: \usepackage{minted}
#+LaTeX_HEADER: \setminted{breaklines}
* Introducción
Configuración realizada en base a la configuración de [[https://daemons.cf/stories/mi-configuracin-de-emacs/][@dryme]]r y [[https://github.com/joedicastro/dotfiles/][@joedicastro]],
varias partes de este archivo han sido tomados de esos repositorios, gracias por
compartirlos.
* Configuración de Paquetes
** Reescribir init.org cuando existen cambios
#+BEGIN_SRC emacs-lisp
  ;;; originaly seen at
  ;;; https://github.com/larstvei/dot-emacs/blob/master/init.org
  (defun tangle-init ()
    "If the current buffer is 'config.org' the code-blocks are
     tangled, and the tangled file is compiled."
    (when (or
         (equal (buffer-file-name)
                (expand-file-name (concat user-emacs-directory "config.org")))
         (equal (buffer-name)
                "config.org"))
      (call-process-shell-command
       "/usr/bin/emacs ~/.emacs.d/config.org --batch --eval='(org-babel-tangle)' && notify-send -a 'Emacs' 'init file tangled'" nil 0)))

  (add-hook 'after-save-hook 'tangle-init)
#+END_SRC
** Repositorios
#+BEGIN_SRC emacs-lisp
  (setq package-archives
        '(("gnu" . "http://elpa.gnu.org/packages/")
          ("melpa" . "http://melpa.org/packages/")
          ("melpa-stable" . "http://stable.melpa.org/packages/")
          ("org" . "http://orgmode.org/elpa/")))
#+END_SRC
** use-package y bind-key
#+BEGIN_SRC emacs-lisp
  ;; avoid problems with files newer than their byte-compiled counterparts
  ;; it's better a lower startup than load an outdated and maybe bugged package
  (setq load-prefer-newer t)
  ;; initialize the packages and create the packages list if not exists
  (package-initialize)
  (when (not package-archive-contents)
    (package-refresh-contents))

  ;; install use-package if not exists
  (unless (package-installed-p 'use-package)
    (package-install 'use-package))

  (eval-when-compile
    (require 'use-package))
  (require 'diminish)                ;; if you use :diminish
  (require 'bind-key)                ;; if you use any :bind variant
#+END_SRC
** Validacion de Esquema
#+BEGIN_SRC emacs-lisp
  (use-package validate
    :ensure t)
#+END_SRC
* Capacidades incorporadas
** Básico
#+BEGIN_SRC emacs-lisp
  (validate-setq inhibit-startup-screen t
                 initial-scratch-message nil
                 large-file-warning-threshold 100000000)

  (menu-bar-mode -1)
  (tool-bar-mode -1)
  (scroll-bar-mode -1)
  (fset 'yes-or-no-p 'y-or-n-p)
  (toggle-indicate-empty-lines)
  (delete-selection-mode)
  (blink-cursor-mode -1)
  (fringe-mode '(1 . 1))
  (add-hook 'before-save-hook 'delete-trailing-whitespace)
  (set-default 'truncate-lines t)

  (validate-setq auto-save-default nil
                 auto-save-list-file-prefix nil
                 make-backup-files nil)

  (global-set-key "\M-p" 'backward-paragraph)
  (global-set-key "\M-n" 'forward-paragraph)

  (setq-default indent-tabs-mode nil)

  (global-set-key (kbd "RET") 'newline-and-indent)
  (global-set-key (kbd "<f5>") 'revert-buffer)
#+END_SRC
** Cache
*** Directorios
#+BEGIN_SRC emacs-lisp
  (defvar bin-dir (expand-file-name "~/.bin/"))
  (defvar tmp-dir (expand-file-name (concat user-emacs-directory "tmp/")))
  (unless (file-exists-p tmp-dir)
    (make-directory tmp-dir))

  (setq-default tramp-persistency-file-name (concat tmp-dir "tramp")
                bookmark-default-file (concat tmp-dir "bookmarks")
                semanticdb-default-save-directory (concat tmp-dir "semanticdb")
                url-configuration-directory (concat tmp-dir "url")
                eshell-directory-name (concat tmp-dir "eshell" ))
#+END_SRC
*** abbrev
#+BEGIN_SRC emacs-lisp
  (use-package abbrev
    :diminish abbrev-mode
    :config
    (abbrev-mode t)
    (setq abbrev-file-name (concat tmp-dir "abbrev.el")
         default-abbrev-mode t
         save-abbrevs t)
    (when (file-exists-p abbrev-file-name)
      (quietly-read-abbrev-file)))
#+END_SRC
*** custom.el
#+BEGIN_SRC emacs-lisp
  (validate-setq custom-file (concat tmp-dir "custom.el"))
  (unless (file-exists-p custom-file)
    (with-temp-buffer
      (write-file custom-file)))
  (load custom-file)
#+END_SRC
*** Historial
#+BEGIN_SRC emacs-lisp
  (setq-default history-length 1000)
  (setq savehist-file (concat tmp-dir "history")
        history-delete-duplicates t
        savehist-save-minibuffer-history 1
        savehist-additional-variables
        '(kill-ring
          search-ring
          regexp-search-ring))
  (savehist-mode t)
#+END_SRC
*** recentf
#+BEGIN_SRC emacs-lisp
  (use-package recentf
    :init
    (recentf-mode)
    :config
    (validate-setq recentf-save-file (concat tmp-dir "recentf.el")
                   recentf-max-saved-items 30
                   recentf-max-menu-items 20))
#+END_SRC
*** Saveplace
#+BEGIN_SRC emacs-lisp
  (use-package saveplace
    :ensure t
    :init
    (save-place-mode)
    :config
    (progn
      (validate-setq save-place-file (concat tmp-dir "saveplace.el"))
      (setq-default save-place t)))
#+END_SRC
*** smex
#+BEGIN_SRC emacs-lisp
  (use-package smex
    :ensure t
    :config
    (validate-setq smex-save-file (concat tmp-dir "smex-items")))
#+END_SRC
** Tramp
#+BEGIN_SRC emacs-lisp
  (use-package tramp
    :config
    (validate-setq tramp-default-method "ssh"))
#+END_SRC
** Modo Mayor por defecto
#+BEGIN_SRC emacs-lisp
  (validate-setq major-mode 'text-mode
                 current-language-environment "Spanish")
#+END_SRC
** Revisión Ortográfica
#+BEGIN_SRC emacs-lisp
  (use-package flyspell
    :diminish (flyspell-mode . " ?")
    :bind (("C-c t s" . flyspell-mode)
           ("C-c l b" . flyspell-buffer)
           :map flyspell-mode-map
           ("\M-\t" . nil)
           ([down-mouse-2] . nil)
           ([mouse-2] . nil))
    :init
    (dolist (hook '(TeX-mode-hook LaTeX-mode-hook text-mode-hook message-mode-hook markdown-mode-hook org-mode-hook))
      (add-hook hook 'turn-on-flyspell))
    (add-hook 'prog-mode-hook 'flyspell-prog-mode)
    :config
    (validate-setq flyspell-use-meta-tab nil
                   flyspell-issue-welcome-flag nil
                   flyspell-issue-message-flag nil)
    (setq-default ispell-program-name    "hunspell"
                  ispell-really-hunspell t
                  ispell-check-comments  t
                  ispell-extra-args      '("-i" "utf-8")
                  ispell-dictionary      "en_US")

    (defun switch-dictionary ()
      (interactive)
      (let* ((dic ispell-current-dictionary)
             (change (if (string= dic "en_US") "es_ES" "en_US")))
        (ispell-change-dictionary change)
        (message "Dictionary switched from %s to %s" dic change)))

    (defun turn-on-spell-check ()
      (flyspell-mode 1))

    (global-set-key (kbd "M-i") 'switch-dictionary)

    (use-package flyspell-correct-ivy
      :ensure t)

    (use-package flyspell-correct
      :ensure t
      :after flyspell
      :bind (:map flyspell-mode-map
                  ("C-c s" . flyspell-correct-word-generic))
      :config
      (validate-setq flyspell-correct-interface 'flyspell-correct-ivy)))
#+END_SRC
** Datos Personales
#+BEGIN_SRC emacs-lisp
  (validate-setq user-full-name "arkhan"
		 user-mail-address "e@ibañez.org")
#+END_SRC
** IBuffer
#+BEGIN_SRC emacs-lisp
  (use-package ibuffer
    :bind ("C-x C-b" . ibuffer))
  (defalias 'list-buffers 'ibuffer)
#+END_SRC
** Ivy
#+BEGIN_SRC emacs-lisp
  (use-package ivy
    :ensure t
    :diminish ivy-mode
    :bind (:map ivy-mode-map
		("C-'" . ivy-avy))
    :config
    (ivy-mode 1)
    (validate-setq ivy-wrap t
                   ivy-virtual-abbreviate 'full
                   ivy-use-virtual-buffers t
                   ivy-count-format "(%d/%d) "
                   ivy-re-builders-alist
                   '((read-file-name-internal . ivy--regex-fuzzy)
                     (t . ivy--regex-plus)))
    (use-package flx :ensure t)
    (setq ivy-on-del-error-function nil
          ivy-initial-inputs-alist nil))
#+END_SRC
** swiper
#+BEGIN_SRC emacs-lisp
  (use-package swiper
    :ensure t
    :bind
    (("C-s" . swiper)
     ("C-r" . swiper)))
#+END_SRC
** consusel
#+BEGIN_SRC emacs-lisp
  (use-package counsel
    :ensure t
    :bind
    (("M-x" . counsel-M-x)
     ("C-c b" . counsel-imenu)
     ("C-x C-f" . counsel-find-file)
     ("C-h f" . counsel-describe-function)
     ("C-h v" . counsel-describe-variable)
     ("C-h b" . counsel-descbinds)
     ("M-y" . counsel-yank-pop)
     ("C-x r r" . counsel-pt))
    :config
    (validate-setq counsel-find-file-at-point t))
#+END_SRC
** Avy
#+BEGIN_SRC emacs-lisp
  (use-package avy
    :ensure t
    :config
    (validate-setq avy-keys       '(?a ?s ?d ?e ?f ?g ?r ?v ?h ?j ?k ?l ?n ?m ?u)
	  avy-background t
	  avy-all-windows t
	  avy-style 'at-full
	  avy-case-fold-search nil)
    (set-face-attribute 'avy-lead-face nil :foreground "gold" :weight 'bold :background nil)
    (set-face-attribute 'avy-lead-face-0 nil :foreground "deep sky blue" :weight 'bold :background nil)
    (use-package ace-link
      :ensure t
      :defer 1
      :config
      (ace-link-setup-default))
    (use-package ace-window
      :ensure t
      :defer 1
      :config
      (set-face-attribute 'aw-leading-char-face nil :foreground "deep sky blue" :weight 'bold :height 2.0)
      (set-face-attribute 'aw-mode-line-face nil :inherit 'mode-line-buffer-id :foreground "lawn green")
      (validate-setq aw-keys   '(?a ?s ?d ?f ?j ?k ?l)
	    aw-dispatch-always t
	    aw-dispatch-alist
	    '((?x aw-delete-window     "Ace - Delete Window")
	      (?c aw-swap-window       "Ace - Swap Window")
	      (?n aw-flip-window)
	      (?h aw-split-window-vert "Ace - Split Vert Window")
	      (?v aw-split-window-horz "Ace - Split Horz Window")
	      (?m delete-other-windows "Ace - Maximize Window")
	      (?g delete-other-windows)
	      (?b balance-windows)
	      (?u winner-undo)
	      (?r winner-redo)))))
#+END_SRC
** Undo Tree
#+BEGIN_SRC emacs-lisp
  (use-package undo-tree
    :ensure t
    :diminish undo-tree-mode
    :init
    (progn
      (global-undo-tree-mode)
      (setq undo-tree-dir (concat tmp-dir "undo/"))
      (make-directory undo-tree-dir :parents)
      (validate-setq undo-tree-history-directory-alist `(("." . ,undo-tree-dir))
            undo-tree-auto-save-history t
            undo-tree-visualizer-timestamps t
            undo-tree-visualizer-diff t))
    (add-hook 'write-file-functions #'undo-tree-save-history-hook)
    (add-hook 'find-file-hook #'undo-tree-load-history-hook))
#+END_SRC
** Orgmode
#+BEGIN_SRC emacs-lisp
  (use-package org
    :ensure org-plus-contrib
    :pin org
    :defer 1
    :config
    (progn
      (add-hook 'org-mode-hook
                (lambda ()
                  (org-bullets-mode 1)
                  (turn-on-auto-fill)
                  (switch-dictionary)))
      ;; set the modules enabled by default
      (setq org-modules
            '(org-bbdb
              org-bibtex
              org-docview
              org-mhe
              org-rmail
              org-crypt
              org-protocol
              org-gnus
              org-id
              org-info
              org-habit
              org-irc
              org-annotate-file
              org-eval
              org-expiry
              org-man
              org-panel
              org-toc))

      ;; set default directories
      (setq org-directory "~/org"
            org-default-notes-file (concat org-directory "/notes.org"))

      ;; set the archive
      (setq org-archive-location "~/org/archive/%s_archive::datetree/** Archived")
      (setq org-agenda-custom-commands
            '(("Q" . "Custom queries") ;; gives label to "Q"
              ("Qa" "Archive search" search ""
               ((org-agenda-files (file-expand-wildcards "~/org/archive/*.org_archive"))))
              ;; ...other commands here
              ))

      ;; highlight code blocks syntax
      (setq org-src-fontify-natively  t
            org-src-tab-acts-natively t)
      (add-to-list 'org-src-lang-modes (quote ("dot" . graphviz-dot)))

      ;; more sane emphasis regex to export to HTML as substitute of Markdown
      (org-set-emph-re 'org-emphasis-regexp-components
                       '(" \t({"
                         "- \t.,:!?;)}[:multibyte:]"
                         " \t\r\n,"
                         "."
                         1))

      ;; highlight code blocks syntax in PDF export
      ;; Include the latex-exporter
      (use-package ox-latex)
      ;; Add minted to the defaults packages to include when exporting.
      (add-to-list 'org-latex-packages-alist '("" "minted"))
      (add-to-list 'org-latex-packages-alist '("" "xunicode"))
      (add-to-list 'org-latex-packages-alist '("" "titletoc"))
      ;; Tell the latex export to use the minted package for source
      ;; code coloration.
      (setq org-latex-listings 'minted)
      ;; Let the exporter use the -shell-escape option to let latex
      ;; execute external programs.
      (setq org-latex-pdf-process
            '("~/.bin/ctex %f"))
      ;; tasks management
      (setq org-log-done t)
      (setq org-clock-idle-time nil)

      ;; agenda & diary
      (setq org-agenda-include-diary t)
      (setq org-agenda-files '("~/org"))
      (setq org-agenda-inhibit-startup t)

      ;; configure the external apps to open files
      (setq org-file-apps
            '(("\\.pdf\\'" . "zathura %s")
              ("\\.gnumeric\\'" . "gnumeric %s")))

      ;; protect hidden trees for being inadvertily edited (do not work with evil)
      (setq-default org-catch-invisible-edits  'error
                    org-ctrl-k-protect-subtree 'error)

      ;; show images inline
      ;; only works in GUI, but is a nice feature to have
      (when (window-system)
        (setq org-startup-with-inline-images t))
      ;; limit images width
      (setq org-image-actual-width '(800))

      ;; :::::: Org-Babel ::::::

      ;; languages supported
      (org-babel-do-load-languages
       (quote org-babel-load-languages)
       (quote ((calc . t)
               (dot . t)
               (emacs-lisp . t)
               (latex . t)
               (org . t)
               (makefile . t)
               (plantuml . t)
               (python . t)
               (sh . t)
               (sqlite . t)
               (sql . t))))
      (setq org-babel-python-command "python2")

      ;; refresh images after execution
      (add-hook 'org-babel-after-execute-hook 'org-redisplay-inline-images)

      ;; don't ask confirmation to execute "safe" languages
      (defun org-confirm-babel-evaluate (lang body)
        (and (not (string= lang "ditaa"))
             (not (string= lang "dot"))
             (not (string= lang "gnuplot"))
             (not (string= lang "ledger"))
             (not (string= lang "plantuml"))))

      (setq org-confirm-babel-evaluate 'org-confirm-babel-evaluate)))
#+END_SRC
** TOC
#+BEGIN_SRC emacs-lisp
  (use-package toc-org
    :ensure t
    :init
    (add-hook 'org-mode-hook 'toc-org-enable))
#+END_SRC
** Clipboard
#+BEGIN_SRC emacs-lisp
  (use-package cliphist
    :ensure t
    :bind* ("C-c v" . cliphist-paste-item)
    :config
    (setq cliphist-linux-clipboard-managers '("clipit")
          cliphist-cc-kill-ring t
          cliphist-use-ivy t))
#+END_SRC
** Scroll
#+BEGIN_SRC emacs-lisp
  (validate-setq scroll-step 1
		 scroll-margin 80
		 scroll-conservatively 3000
		 scroll-up-aggressively 0.02
		 scroll-down-aggressively 0.02)

  (use-package smooth-scrolling
    :ensure t
    :config
    (validate-setq smooth-scroll-margin 5))
#+END_SRC
** Async
#+BEGIN_SRC emacs-lisp
  (use-package async
    :defer t
    :ensure t
    :config
    (setq async-bytecomp-package-mode t))
#+END_SRC
** Pass
#+BEGIN_SRC emacs-lisp
(use-package pass
  :ensure t
  :defer t)
#+END_SRC
* UI
** Frame Title
#+BEGIN_SRC emacs-lisp
  (setq-default frame-title-format (list (user-login-name) "@" (system-name) " %b [%m]"))
#+END_SRC
** Cursor
#+BEGIN_SRC emacs-lisp
  (setq-default cursor-type '(hbar . 2))

  (use-package frame
    :config
    (defun set-cursor-hook (frame)
      (modify-frame-parameters
       frame (list (cons 'cursor-color "white"))))

    (add-hook 'after-make-frame-functions 'set-cursor-hook))
#+END_SRC
** NeoTree
#+BEGIN_SRC emacs-lisp
  (use-package neotree
    :ensure t
    :bind* ("M-1" . neotree-toggle)
    :bind
    (:map neotree-mode-map
          ("<C-return>" . neotree-change-root)
          ("C" . neotree-change-root)
          ("c" . neotree-create-node)
          ("+" . neotree-create-node)
          ("d" . neotree-delete-node)
          ("r" . neotree-rename-node))
    :config
    (setq neo-theme 'ascii
          neo-vc-integration nil
          neo-window-width 32
          neo-create-file-auto-open t
          neo-banner-message nil
          neo-mode-line-type 'neotree
          neo-dont-be-alone t
          neo-persist-show t
          neo-show-updir-line nil
          neo-show-hidden-files nil
          neo-auto-indent-point t
          neo-hidden-regexp-list '(".DS_Store" ".pyc" ".tern-port" ".git/*" "node_modules/*" ".meteor" "_build" "deps")))
#+END_SRC
** Smart Mode Line
#+BEGIN_SRC emacs-lisp
  (validate-setq line-number-mode t
                 column-number-mode t)

  (use-package smart-mode-line
    :ensure t
    :config
    (add-hook 'after-load-theme-hook 'smart-mode-line-enable)
    (setq sml/vc-mode-show-backend t
          sml/mode-width 'full
          sml/shorten-modes t
          sml/no-confirm-load-theme t
          sml/theme 'dark)
    (sml/setup))
    #+END_SRC
** Tipografia
#+BEGIN_SRC emacs-lisp
  (set-face-attribute 'default nil :family "Hack" :height 80)
  (set-fontset-font "fontset-default" nil
    (font-spec :size 20 :name "Symbola"))
#+END_SRC
** Theme
#+BEGIN_SRC emacs-lisp
  (use-package doom-themes
    :ensure t
    :init
    (load-theme 'doom-molokai t)
    :config
    (add-hook 'find-file-hook 'doom-buffer-mode)
    (add-hook 'minibuffer-setup-hook 'doom-brighten-minibuffer)
    (validate-setq org-fontify-whole-heading-line t
                   org-fontify-done-headline t
                   org-fontify-quote-and-verse-blocks t)
    (when (display-graphic-p)
      (require 'doom-neotree)
      (validate-setq doom-neotree-file-icons nil)))
#+END_SRC
** UTF-8
#+BEGIN_SRC emacs-lisp
  (prefer-coding-system       'utf-8)
  (set-default-coding-systems 'utf-8)
  (set-terminal-coding-system 'utf-8)
  (set-keyboard-coding-system 'utf-8)
  (validate-setq x-select-request-type
                 '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
#+END_SRC
** EShell
#+BEGIN_SRC emacs-lisp
  (use-package eshell
    :defer 5
    :bind ("C-x m" . eshell)
    :config
    (use-package em-smart)
    (setq eshell-where-to-jump 'begin)
    (setq eshell-review-quick-commands nil)
    (setq eshell-smart-space-goes-to-end t)
    ;; (add-to-list 'eshell-visual-commands "opam --help")
    (add-hook 'eshell-mode-hook 'eshell-smart-initialize))
#+END_SRC
** SSH
#+BEGIN_SRC emacs-lisp
  (use-package ssh
    :ensure t
    :init
    (add-hook 'ssh-mode-hook
              (lambda ()
                (setq ssh-directory-tracking-mode t)
                (shell-dirtrack-mode t)
                (setq dirtrackp nil))))
#+END_SRC
* Programación
** Configuración genérica
*** Aggressive Indent
#+BEGIN_SRC emacs-lisp
  (use-package aggressive-indent
    :ensure t
    :config
    (add-hook 'prog-mode-hook #'aggressive-indent-mode))
    #+END_SRC
*** Company
#+BEGIN_SRC emacs-lisp
  (use-package company
    :ensure t
    :bind
    ("C-c y" . company-yasnippet)
    :config
    (add-hook 'after-init-hook 'global-company-mode)
    (validate-setq company-echo-delay 0
                   company-idle-delay 0.2
                   company-minimum-prefix-length 1
                   company-tooltip-align-annotations t
                   company-tooltip-limit 20
                   company-transformers '(company-sort-by-occurrence))
    (set-face-attribute 'company-preview nil :foreground "black" :underline t)
    (set-face-attribute 'company-preview-common nil :inherit 'company-preview)
    (set-face-attribute 'company-tooltip nil :background "lightgray" :foreground "black")
    (set-face-attribute 'company-tooltip-selection nil :background "steelblue" :foreground "white")
    (set-face-attribute 'company-tooltip-common nil :foreground "darkgreen" :weight 'bold)
    (set-face-attribute 'company-tooltip-common-selection nil :foreground "black" :weight 'bold)

    (defun company-yasnippet-or-completion ()
      "Solve company yasnippet conflicts."
      (interactive)
      (let ((yas-fallback-behavior
             (apply 'company-complete-common nil)))
        (yas-expand)))

    (add-hook 'company-mode-hook
              (lambda ()
                (substitute-key-definition
                 'company-complete-common
                 'company-yasnippet-or-completion
                 company-active-map))))
#+END_SRC
*** Duplicar Lineas o Región
#+BEGIN_SRC emacs-lisp
  (defun duplicate-current-line-or-region (arg)
    "Duplicates the current line or region ARG times.
  If there's no region, the current line will be duplicated. However, if
  there's a region, all lines that region covers will be duplicated."
    (interactive "p")
    (let (beg end (origin (point)))
      (if (and mark-active (> (point) (mark)))
          (exchange-point-and-mark))
      (setq beg (line-beginning-position))
      (if mark-active
          (exchange-point-and-mark))
      (setq end (line-end-position))
      (let ((region (buffer-substring-no-properties beg end)))
        (dotimes (i arg)
          (goto-char end)
          (newline)
          (insert region)
          (setq end (point)))
        (goto-char (+ origin (* (length region) arg) arg)))))
  (global-set-key (kbd "C-c d") 'duplicate-current-line-or-region)
#+END_SRC
*** EditorConfig
#+BEGIN_SRC emacs-lisp
  (use-package editorconfig
    :ensure t
    :diminish (editorconfig-mode . "ec")
    :config
    (editorconfig-mode))
#+END_SRC
*** Flycheck
#+BEGIN_SRC emacs-lisp
  (use-package flycheck
    :ensure t
    :bind
    (("C-c e n" . flycheck-next-error)
     ("C-c e p" . flycheck-previous-error))
    :config
    (add-hook 'after-init-hook #'global-flycheck-mode))
#+END_SRC
*** Git
#+BEGIN_SRC emacs-lisp
  (setq vc-follows-symlinks t
        find-file-visit-truename t)

  (use-package magit
    :ensure t
    :bind
    ("C-x g" . magit-status)
    ("C-x C-g l" . magit-file-log)
    ("C-x f" . magit-grep)
    :init
    (progn
      (setq magit-git-executable (concat bin-dir "tg"))
      (use-package magit-blame
        :bind ("C-c C-g b" . magit-blame-mode))
      (delete 'Git vc-handled-backends)
      (defadvice magit-status (around magit-fullscreen activate)
        (window-configuration-to-register :magit-fullscreen)
        ad-do-it
        (delete-other-windows))
      (defadvice git-commit-commit (after delete-window activate)
        (delete-window))
      (defadvice git-commit-abort (after delete-window activate)
        (delete-window))
      (defun magit-commit-mode-init ()
        (when (looking-at "\n")
          (open-line 1))))
    :config
    (progn
      (defadvice magit-quit-window (around magit-restore-screen activate)
        (let ((current-mode major-mode))
          ad-do-it
          (when (eq 'magit-status-mode current-mode)
            (jump-to-register :magit-fullscreen))))
      (defun magit-maybe-commit (&optional show-options)
        "Runs magit-commit unless prefix is passed"
        (interactive "P")
        (if show-options
            (magit-key-mode-popup-committing)
          (magit-commit)))
      (define-key magit-mode-map "c" 'magit-maybe-commit)
      (use-package rebase-mode)
      (setq magit-completing-read-function 'ivy-completing-read
            magit-default-tracking-name-function 'magit-default-tracking-name-branch-only
            magit-status-buffer-switch-function 'switch-to-buffer
            magit-diff-refine-hunk t
            magit-rewrite-inclusive 'ask
            magit-save-some-buffers t
            magit-process-popup-time 10
            magit-set-upstream-on-push 'askifnotset
            magit-refs-show-commit-count 'all
            magit-log-buffer-file-locket t)))

  (use-package git-gutter-fringe
    :ensure t
    :diminish (git-gutter-mode . "gg")
    :bind (("C-x C-g" . git-gutter)
           ("C-x v =" . git-gutter:popup-hunk)
           ;; Jump to next/previous hunk
           ("C-x p" . git-gutter:previous-hunk)
           ("C-x n" . git-gutter:next-hunk)
           ;; Stage current hunk
           ("C-x v s" . git-gutter:stage-hunk)
           ;; Revert current hunk
           ("C-x v r" . git-gutter:revert-hunk)
           ;; Mark current hunk
           ("C-x v SPC" . git-gutter:mark-hunk))
    :init
    (global-git-gutter-mode))

  (use-package gitconfig-mode
    :ensure t
    :mode ("/\\.?git/?config$"
           "/\\.gitmodules$")
    :init (add-hook 'gitconfig-mode-hook 'flyspell-mode))

  (use-package gitignore-mode
    :ensure t
    :mode ("/\\.gitignore$"
           "/\\.git/info/exclude$"
           "/git/ignore$"))

  (use-package gitattributes-mode
    :ensure t
    :defer t)

  (use-package git-timemachine
    :ensure t
    :commands git-timemachine
    :config
    (progn
      (bind-keys :map git-timemachine-mode
                 ("c" . git-timemachine-show-current-revision)
                 ("b" . git-timemachine-switch-branch))))
#+END_SRC
*** Origami
#+BEGIN_SRC emacs-lisp
  (use-package origami
    :ensure t
    :bind (:map origami-mode-map
                ("M-/" . origami-toggle-all-nodes)
                ("M-+" . origami-recursively-toggle-node))
    :init
    (global-origami-mode))
#+END_SRC
*** Parents
#+BEGIN_SRC emacs-lisp
  (electric-pair-mode 1)

  (use-package paren
    :init
    (show-paren-mode)
    :config
    (set-face-background 'show-paren-match (face-background 'default))
    (set-face-foreground 'show-paren-match "#def")
    (set-face-attribute 'show-paren-match nil :weight 'extra-bold))

  (use-package smartparens
    :ensure t
    :commands
    (smartparens-mode
     smartparens-strict-mode)
    :bind
    (:map smartparens-strict-mode-map
          ("C-}" . sp-forward-slurp-sexp)
          ("M-s" . sp-backward-unwrap-sexp)
          ("C-c [" . sp-select-next-thing)
          ("C-c ]" . sp-select-next-thing-exchange))
    :config
    (require 'smartparens-config))

  (use-package rainbow-delimiters
    :ensure t
    :config
    (add-hook 'prog-mode-hook 'rainbow-delimiters-mode))
#+END_SRC
*** Pretty Symbols
#+BEGIN_SRC emacs-lisp
  (use-package pretty-symbols
    :ensure t
    :config (global-prettify-symbols-mode 1))
#+END_SRC
*** Rainbow
#+BEGIN_SRC emacs-lisp
  (use-package rainbow-mode
    :ensure t
    :config
    (add-hook 'prog-mode-hook 'rainbow-mode)
    (add-hook 'conf-mode-hook 'rainbow-mode))
#+END_SRC
*** Yasnippet
#+BEGIN_SRC emacs-lisp
  (use-package yasnippet
    :ensure t
    :init
    (yas-global-mode)
    :config
    (validate-setq yas-snippet-dirs
                   '("~/.emacs.d/snippets/collection"
                     "~/.emacs.d/snippets/yasnippet-snippets")))
#+END_SRC
*** ECB
#+BEGIN_SRC emacs-lisp
  (use-package ecb
    :ensure t
    :bind (;; activate and deactivate ecb
           ("C-x C-;" . ecb-activate)
           ("C-x C-'" . ecb-deactivate)
           ;; show/hide ecb window
           ("C-;" . ecb-show-ecb-windows)
           ("C-'" . ecb-hide-ecb-windows)
           ;; quick navigation between ecb windows
           ("C-)" . ecb-goto-window-edit1)
           ("C-!" . ecb-goto-window-directories)
           ("C-@" . ecb-goto-window-sources)
           ("C-#" . ecb-goto-window-methods)
           ("C-$" . ecb-goto-window-compilation))
    :config
    (setq ecb-layout-name "right1"))
#+END_SRC
*** PO
#+BEGIN_SRC emacs-lisp
  (use-package po-mode
    :ensure t
    :config
    ;; Fuente: https://www.emacswiki.org/emacs/PoMode
    (defun po-wrap ()
      "Filter current po-mode buffer through `msgcat' tool to wrap all lines."
      (interactive)
      (if (eq major-mode 'po-mode)
          (let ((tmp-file (make-temp-file "po-wrap."))
                (tmp-buf (generate-new-buffer "*temp*")))
            (unwind-protect
                (progn
                  (write-region (point-min) (point-max) tmp-file nil 1)
                  (if (zerop
                       (call-process
                        "msgcat" nil tmp-buf t (shell-quote-argument tmp-file)))
                      (let ((saved (point))
                            (inhibit-read-only t))
                        (delete-region (point-min) (point-max))
                        (insert-buffer tmp-buf)
                        (goto-char (min saved (point-max))))
                    (with-current-buffer tmp-buf
                      (error (buffer-string)))))
              (kill-buffer tmp-buf)
              (delete-file tmp-file)))))

    (defun po-guess-language ()
      "Return the language related to this PO file."
      (save-excursion
        (goto-char (point-min))
        (re-search-forward po-any-msgstr-block-regexp)
        (goto-char (match-beginning 0))
        (if (re-search-forward
             "\n\"Language: +\\(.+\\)\\\\n\"$"
             (match-end 0) t)
            (po-match-string 1))))

    (defadvice po-edit-string (around setup-spell-checking (string type expand-tabs) activate)
      "Set up spell checking in subedit buffer."
      (let ((po-language (po-guess-language)))
        ad-do-it
        (if po-language
            (progn
              (ispell-change-dictionary po-language)
              (turn-on-flyspell)
              (flyspell-buffer))))))
#+END_SRC
** Lenguajes de Programación
*** LaTex
#+BEGIN_SRC emacs-lisp
  (use-package tex
    :defer t
    :ensure auctex
    :bind
    ("C-c c" . TeX-clean)
    :init
    (progn
      (require 'smartparens-latex)
      (add-hook 'TeX-mode-hook
                (lambda ()
                  (outline-minor-mode t)
                  (flyspell-mode t)
                  (TeX-PDF-mode t)
                  (TeX-fold-mode t)
                  (switch-dictionary)))
      (add-hook 'LaTeX-mode-hook
                (lambda ()
                  (LaTeX-math-mode t)
                  (reftex-mode t)))
      (setq TeX-auto-save t
            TeX-parse-self t
            LaTeX-syntactic-comment t
            TeX-save-query nil
            TeX-PDF-mode t
            TeX-auto-untabify t)
      (setq-default TeX-engine 'xetex))
    :config
    (add-hook 'TeX-mode-hook 'turn-on-auto-fill)
    (add-hook 'LaTeX-mode-hook 'turn-on-auto-fill)
    (use-package cdlatex
      :ensure t)
    (add-hook 'LaTeX-mode-hook 'turn-on-cdlatex)

    (setq TeX-view-program-list '(("zathura" "zathura %o"))
          TeX-view-program-selection '((output-pdf "zathura")))

    ;; set XeTeX mode in TeX/LaTeX
    (add-hook 'LaTeX-mode-hook
              (lambda () (push
                     '("cTeX" "%(ctex_bin) %t " TeX-run-TeX nil t
                       :help "Compilation with custom script") TeX-command-list)
                (add-to-list 'TeX-expand-list
                             '("%(ctex_bin)" (lambda ()
                                               (concat bin-dir "ctex" ))))
                (setq TeX-command-default "cTeX"
                      TeX-save-query nil
                      TeX-show-compilation t))))

  (use-package reftex
    :commands turn-on-reftex
    :init
    (progn
      (setq reftex-plug-into-AUCTeX t)))

  (use-package bibtex
    :init
    (progn
      (setq bibtex-align-at-equal-sign t)
      (add-hook 'bibtex-mode-hook
                (lambda ()
                  (set-fill-column 120)))))

  (use-package company-auctex
    :ensure t
    :init
    (company-auctex-init))
#+END_SRC
*** Lua
#+BEGIN_SRC emacs-lisp
  (use-package lua-mode
    :ensure t
    :mode "\\.lua\\'")

  (use-package company-lua
    :ensure t
    :init
    (defun my/lua-mode-hook ()
      (add-to-list 'company-backends 'company-lua))

    (add-hook 'lua-mode-hook 'my/lua-mode-hook))
#+END_SRC
*** Python
#+BEGIN_SRC emacs-lisp
  (use-package elpy
    :ensure t
    :pin melpa-stable
    :config
    (elpy-enable)
    (validate-setq elpy-rpc-backend "jedi"
                   elpy-modules (dolist (elem
                                         '(elpy-module-sane-defaults
                                           elpy-module-company
                                           elpy-module-eldoc
                                           elpy-module-highlight-indentation
                                           elpy-module-pyvenv
                                           elpy-module-yasnippet))))
    (when (executable-find "ipython")
      (elpy-use-ipython)
      (validate-setq python-shell-interpreter-args "--simple-prompt --pprint"))
    (require 'smartparens-python)
    (when (require 'flycheck nil t)
      (validate-setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
      (add-hook 'python-mode-hook 'flycheck-mode))

    (use-package company-jedi
      :defer t
      :ensure t
      :init
      (setq jedi:complete-on-dot t)
      (setq jedi:get-in-function-call-delay 0.2)
      (defun my/python-mode-hook ()
        (add-to-list 'company-backends 'company-jedi))

      (add-hook 'python-mode-hook 'my/python-mode-hook))

    (use-package py-autopep8
      :ensure t
      :config
      (add-hook 'python-mode-hook 'py-autopep8-enable-on-save))

    (use-package py-isort
      :ensure t
      :config
      (setq py-sort-options '("--lines=100"))
      (add-hook 'before-save-hook 'py-isort-before-save))

    (use-package virtualenvwrapper
      :ensure t
      :commands (venv-workon venv-deactivate venv-initialize-interactive-shells venv-initialize-eshell)
      :init
      (venv-initialize-interactive-shells)
      (venv-initialize-eshell)
      (setq venv-location "~/.virtualenvs")))
#+END_SRC
*** SQL
#+BEGIN_SRC emacs-lisp
  (use-package sql-mode
    :mode "\\.sql$\\'"
    :init
    (add-to-list 'same-window-buffer-names "*SQL*")
    (add-hook 'sql-interactive-mode-hook
              (lambda ()
                (toggle-truncate-lines t)
                (validate-setq sql-prompt-regexp "^[_[:alpha:]]*[=][#>] "
                               sql-prompt-cont-regexp "^[_[:alpha:]]*[-][#>] "))))

  (use-package sqlup-mode
    :ensure t
    :bind
    ("C-c u" . sqlup-capitalize-keywords-in-region)
    :init
    (add-hook 'sql-mode-hook 'sqlup-mode)
    (add-hook 'sql-interactive-mode-hook 'sqlup-mode))
#+END_SRC
*** XML
#+BEGIN_SRC emacs-lisp
  (use-package nxml-mode
    :mode (("\\.plist\\'" . nxml-mode)
           ("\\.rss\\'"   . nxml-mode)
           ("\\.svg\\'"   . nxml-mode)
           ("\\.xml\\'"   . nxml-mode)
           ("\\.xsd\\'"   . nxml-mode)
           ("\\.xslt\\'"  . nxml-mode)
           ("\\.pom$"     . nxml-mode))
    :config
    (validate-setq nxml-slash-auto-complete-flag t
                   nxml-auto-insert-xml-declaration-flag t)
    (add-to-list 'magic-mode-alist '("<\\?xml" . nxml-mode))
    (mapc
     (lambda (pair)
       (if (or (eq (cdr pair) 'xml-mode)
               (eq (cdr pair) 'sgml-mode))
           (setcdr pair 'nxml-mode)))
     auto-mode-alist)

    (defun xml-format ()
      (interactive)
      (save-excursion
        (shell-command-on-region (mark) (point) "xmllint --format -" (buffer-name) t))))
#+END_SRC
*** Web
#+BEGIN_SRC emacs-lisp
  (use-package web-mode
    :ensure t
    :mode
    (("\\.phtml\\'" . web-mode)
     ("\\.tpl\\.php\\'" . web-mode)
     ("\\.[agj]sp\\'" . web-mode)
     ("\\.as[cp]x\\'" . web-mode)
     ("\\.erb\\'" . web-mode)
     ("\\.mustache\\'" . web-mode)
     ("\\.djhtml\\'" . web-mode)
     ("\\.html?\\'" . web-mode)))
#+END_SRC
* Varios
** Csv
#+BEGIN_SRC emacs-lisp
  (use-package csv-mode
    :ensure t
    :mode "\\.[Cc][Ss][Vv]\\'")
#+END_SRC
** Nginx
#+BEGIN_SRC emacs-lisp
  (use-package nginx-mode
    :ensure t
    :mode
    ("/nginx/sites-\\(?:available\\|enabled\\)/" . nginx-mode))
#+END_SRC
** Pkgbuil
#+BEGIN_SRC emacs-lisp
  (use-package pkgbuild-mode
    :ensure t
    :mode "PKGBUILD\\'")
#+END_SRC
** Docker
#+BEGIN_SRC emacs-lisp
  (use-package dockerfile-mode
    :mode "Dockerfile\\'"
    :ensure t)
#+END_SRC
** Pentadactyl
#+BEGIN_SRC emacs-lisp
  (use-package dactyl-mode
    :ensure t
    :mode (("\\pentadactylrc\\'" . dactyl-mode)
           ("\\.pentadactylrc\\'" . dactyl-mode)
           ("\\.penta\\'" . dactyl-mode)))
#+END_SRC
** Sudo
#+BEGIN_SRC emacs-lisp
  (use-package sudo-edit
    :ensure t
    :bind ("C-c C-r" . sudo-edit))
#+END_SRC
